package com.dm.web.controller.base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dm.common.annotation.Log;
import com.dm.common.core.controller.BaseController;
import com.dm.common.core.domain.AjaxResult;
import com.dm.common.enums.BusinessType;
import com.dm.base.domain.BDb;
import com.dm.base.service.IBDbService;
import com.dm.common.utils.poi.ExcelUtil;
import com.dm.common.core.page.TableDataInfo;

/**
 * 数据库Controller
 *
 * @author dm
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/base/db")
public class BDbController extends BaseController
{
    @Autowired
    private IBDbService bDbService;

    /**
     * 查询数据库列表
     */
    @PreAuthorize("@ss.hasPermi('base:db:list')")
    @GetMapping("/list")
    public TableDataInfo list(BDb bDb)
    {
        startPage();
        List<BDb> list = bDbService.selectBDbList(bDb);
        return getDataTable(list);
    }

    /**
     * 导出数据库列表
     */
    @PreAuthorize("@ss.hasPermi('base:db:export')")
    @Log(title = "数据库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BDb bDb)
    {
        List<BDb> list = bDbService.selectBDbList(bDb);
        ExcelUtil<BDb> util = new ExcelUtil<BDb>(BDb.class);
        util.exportExcel(response, list, "数据库数据");
    }

    /**
     * 获取数据库详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:db:query')")
    @GetMapping(value = "/{dbId}")
    public AjaxResult getInfo(@PathVariable("dbId") Long dbId)
    {
        return success(bDbService.selectBDbByDbId(dbId));
    }

    /**
     * 新增数据库
     */
    @PreAuthorize("@ss.hasPermi('base:db:add')")
    @Log(title = "数据库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BDb bDb)
    {
        return toAjax(bDbService.insertBDb(bDb));
    }

    /**
     * 修改数据库
     */
    @PreAuthorize("@ss.hasPermi('base:db:edit')")
    @Log(title = "数据库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BDb bDb)
    {
        return toAjax(bDbService.updateBDb(bDb));
    }

    /**
     * 删除数据库
     */
    @PreAuthorize("@ss.hasPermi('base:db:remove')")
    @Log(title = "数据库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable Long[] dbIds)
    {
        return toAjax(bDbService.deleteBDbByDbIds(dbIds));
    }
}
